package one.stand.controller;

import com.jmp.api.admin.dto.LiveListResponse;
import com.jmp.api.admin.util.LabelUtil;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.LiveListRequest;
import com.jmp.base.dto.LiveListVo;
import com.jmp.base.dto.LiveRemoveRequest;
import com.jmp.feign.LiveFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.Company2Dto;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.dto.UserDto;
import one.stand.model.BusinessCard;
import one.stand.model.ResultModel;
import one.stand.service.BusinessCardBaseService;
import one.stand.service.CompanyBaseService;
import one.stand.service.user.UserAdminService;
import one.stand.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(description = "直播管理")
@RestController
@RequestMapping("adminApi/live")
public class LiveController {

    @Autowired
    private LiveFeign liveFeign;

    @Autowired
    private CompanyBaseService companyBaseService;
    @Autowired
    private BusinessCardBaseService businessCardBaseService;
    @Autowired
    private UserAdminService userAdminService;

    @ApiOperation(value = "1直播大厅")
    @GetMapping("list")
    @SecurityAuthCheck
    public ResultModel<LiveListResponse> list(com.jmp.api.admin.dto.LiveListRequest request) {

        LiveListRequest liveListRequest = LiveListRequest.builder()
                .keyword(request.getKeyword())
                .build();
        liveListRequest.setPageNum(request.getPageNum());
        liveListRequest.setPageSize(request.getPageSize());
        ResultModel<LiveListVo> liveListVoResultModel = liveFeign.list(liveListRequest);
        AssertUtil.isTrue(liveListVoResultModel);

        PageResponse2<LiveListResponse.Live> livePage = ConvertUtils.page2(liveListVoResultModel.getData().getLivePage(), live -> {
            BusinessCard businessCard = businessCardBaseService.getCustomer(live.getCardId());
            Company2Dto company = companyBaseService.get2(businessCard.getCompanyId());
            UserDto user = userAdminService.get(businessCard.getUserId());
            return LiveListResponse.Live.builder()
                    .roomId(live.getRoomId())
                    .title(live.getTitle())
                    .photo(live.getPhoto())
                    .status(LabelUtil.getLiveStatus(live.getStatus()))
                    .viewNum(live.getViewNum())//直播中才显示观看人数
                    .time(live.getTime().getTime())

                    .userId(user.getUserId())
                    .userImg(user.getUserImg())
                    .userName(user.getName())

                    .companyId(company.getCompanyId())
                    .companyLogo(company.getCompanyLogo())
                    .companyName(company.getCompanyName())
                    .build();
        });

        LiveListResponse response = LiveListResponse.builder()
                .livePage(livePage)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "2删除直播")
    @PostMapping("remove")
    @SecurityAuthCheck
    public ResultModel remove(@RequestBody com.jmp.api.admin.dto.LiveRemoveRequest request) {
        ResultModel resultModel = liveFeign.remove(LiveRemoveRequest.builder()
                .roomId(request.getRoomId())
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

}
